package dao;

/**
 * GenericDAO.java
 *
 * $Id$
 *
 */
//import devmedia.entity.HibernateUtil;
import dominio.HibernateUtil;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Dyego Souza do Carmo
 * @since
 */
public abstract class GenericDAO {

    private static final long serialVersionUID = 1L;

    /**
     * Construtor padrão
     *
     * @author Dyego Souza do Carmo
     * @version 1.0,
     */
    public GenericDAO() {
    }

    protected org.hibernate.Session getSession() {
        return HibernateUtil.getInstance().getSession();
    }

    public void salvar(Serializable classe) {
        Session ses = (Session) getSession();
        ses.saveOrUpdate(classe);
        ses.getTransaction().commit();
        ses.close();
    }

    public void atualizar(Serializable classe) {
        Session ses = (Session) getSession();
        ses.update(classe);
        ses.getTransaction().commit();
        ses.close();
    }

    protected <T extends Serializable> T getClasse(Class<T> classe, Serializable chave) {
        Session ses = getSession();
        Serializable toReturn = (Serializable) ses.get(classe, chave);
        ses.getTransaction().commit();
        ses.close();
        return (T) toReturn;
    }

    protected void remover(Serializable classe) {
        Session ses = getSession();
        ses.delete(classe);
        ses.getTransaction().commit();
        ses.close();
    }

    protected Serializable getPurePojo(String query, Object... params) {
        Session ses = getSession();

        Query qr = ses.createQuery(query);
        
        for (int i = 1; i <= params.length; i++) {
            qr.setParameter(i-1, params[i-1]);
        }
        Object toReturn = qr.uniqueResult();

        ses.getTransaction().commit();
        ses.close();
        return (Serializable) toReturn;
    }
    

    protected <T extends Serializable> Set<T> getLista(Class<T> classToCast, String query, Object... params) {
        Session ses = getSession();

        Query qr = ses.createQuery(query);

        for (int i = 1; i <= params.length; i++) {
            qr.setParameter(i-1, params[i - 1]);
        }
        @SuppressWarnings("unchecked")
       
        Set<T> toReturn = new HashSet(); 
        toReturn.addAll(qr.list());
        
        
        ses.getTransaction().commit();
        ses.close();
        return toReturn;
    }
    
    protected <T extends Serializable> Set<T> getListaLimitada(Class<T> classToCast, String query, Object... params) {
        Session ses = getSession();

        Query qr = ses.createQuery(query);
        
        qr.setMaxResults(10);

        for (int i = 1; i <= params.length; i++) {
            qr.setParameter(i-1, params[i - 1]);
        }
        @SuppressWarnings("unchecked")
       
        Set<T> toReturn = new HashSet(); 
        toReturn.addAll(qr.list());
        
        ses.getTransaction().commit();
        ses.close();
        return toReturn;
    }
    
  protected int getLote(String query, Object... params) {
        int rows=0;
        Session ses = getSession();

        Query qr = ses.createQuery(query);
        
        for (int i = 1; i <= params.length; i++) {
            qr.setParameter(i-1, params[i-1]);
        }
        
        rows = qr.executeUpdate();
        ses.getTransaction().commit();
        ses.close();
        return rows;
    }
    
    
}